// pages/pay/index.js
import { authRequest } from '../../utils/net';
Page({
  data: {
    //余额
    money: 0.00,
    // 初始化tab        
    currentTab: 0,
    // 初始化pay_money
    pay_money: 100,
    // 是否有押金初始化
    is_deposit: false
  },
  onLoad: function () {
    var that = this;
    authRequest({
      // 获取个人的信息借口
      url: '/my_info',
      method: "GET",
      success: function (res) {
        that.setData({
          // toFixed保留小数
          money: res.data.data.money.toFixed(2),
          is_deposit: res.data.data.is_deposit
        })
      },
      fail: function (res) {
        // fail
      }
    });

  },
  // tab切换
  swichNav: function (e) {
    var that = this;
    // 如果点击标签的时候that.data.currentTab===页面自定义属性的current则反回false不改变颜色，反过来则改变颜色并获取到新的自定义属性的current保存到that.data.currentTab，并提取要充值的数值自定义属性的pay保存到that.data.pay
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        // tab的值
        currentTab: e.target.dataset.current,
        // pay_money要充值的钱
        pay_money: e.target.dataset.money
      })
      console.log('currentTab:' + that.data.currentTab)
      console.log('money:' + that.data.pay_money)
    }
  },
  cz: function () {
    var that = this;
    wx.showModal({
      title: '充值',
      content: '您是否确定充值' + that.data.pay_money + '元？',
      success: function (res) {
        if (res.confirm) {
          authRequest({
            url: '/pay_money',
            data: {
              money: that.data.pay_money
            },
            method: 'POST',
            success: function (res) {
              authRequest({
                url: '/my_info',
                method: "GET",
                success: function (res) {
                  that.setData({
                    money: res.data.data.money.toFixed(2)
                  })
                }
              });
              wx.showToast({
                title: '充值成功',
                icon: 'success',
                duration: 500
              })
            },
            fail: function (res) {
              // fail
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  pledge: function () {
    var that = this;
    if (that.data.is_deposit == false) {
      wx.showModal({
        title: '充值押金',
        content: '您是否确定充值押金299？',
        success: function (res) {
          if (res.confirm) {
            authRequest({
              url: '/deposit',
              method: 'POST',
              success: function (res) {
                authRequest({
                  url: '/my_info',
                  method: "GET",
                  success: function (res) {
                    that.setData({
                      is_deposit: res.data.data.is_deposit
                    })
                  }
                });
                // 显示消息提示框
                wx.showToast({
                  title: '充值成功',
                  icon: 'success',
                  duration: 500
                })

              }
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {

    }
  },
  deposit: function () {
    var that = this;
    authRequest({
      url: '/deposit',
      method: 'DELETE',
      success: function (res) {
        authRequest({
          url: '/my_info',
          method: "GET",
          success: function (res) {
            that.setData({
              is_deposit: res.data.data.is_deposit
            })
          }
        });
        // 显示消息提示框
        wx.showToast({
          title: '退款成功',
          icon: 'success',
          duration: 500
        })
      }
    })
  }
})